﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.Data;
using System.Data;

namespace blackhouse.AsyncMail.Business.Data {
    public class SqlASyncMailDataAccess : IASyncMailDataAccess {

        #region Fields

        private MsSqlAdoDataHelper dataHelper;

        #endregion

        #region Constructors

        public SqlASyncMailDataAccess(string connectionString) {
            this.dataHelper = new MsSqlAdoDataHelper(connectionString);
            SqlVersioning versioning = new SqlVersioning(connectionString, GetType().Assembly);
        }

        #endregion

        #region IASyncMailDataAccess Members

        public int SaveMail(int templateId, string bindObject, Type bindType, string receiverMail, string receiverDisplayName, string messageTitle) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_ASyncMailStockSave";
            this.dataHelper.AddParameter("TemplateId", templateId, System.Data.DbType.Int32);
            this.dataHelper.AddParameter("Object", bindObject, System.Data.DbType.String);
            this.dataHelper.AddParameter("Type", bindType.AssemblyQualifiedName, System.Data.DbType.String);
            this.dataHelper.AddParameter("ReceiverMail", receiverMail, System.Data.DbType.String);
            this.dataHelper.AddParameter("ReceiverDisplayName", receiverDisplayName, System.Data.DbType.String);
            this.dataHelper.AddParameter("MessageTitle", messageTitle, System.Data.DbType.String);
            return Convert.ToInt32(this.dataHelper.ExecuteScalar());
        }

        public ASyncMailData GetTopMail() {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_ASyncMailStockTopGet";
            return this.ReadASyncMailData(this.dataHelper.Execute());
        }

        public ASyncMailData GetMailByRequest(int requestId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_ASyncMailStockGet";
            this.dataHelper.AddParameter("RequestId", requestId, DbType.Int32);
            return this.ReadASyncMailData(this.dataHelper.Execute());
        }

        public void MarkAsSended(int requestId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_ASyncMailStockSended";
            this.dataHelper.AddParameter("RequestId", requestId, DbType.Int32);
            this.dataHelper.ExecuteWithoutResult();
        }

        public void MarkSendError(int requestId, string errorMessage) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_ASyncMailStockError";
            this.dataHelper.AddParameter("RequestId", requestId, DbType.Int32);
            this.dataHelper.AddParameter("ErrorMessage", errorMessage, DbType.String);
            this.dataHelper.ExecuteWithoutResult();
        }

        #endregion

        #region Methods

        private ASyncMailData ReadASyncMailData(IDataReader reader) {
            ASyncMailData data = default(ASyncMailData);
            if (reader.Read())
                data = this.ReadASyncMailDataFields(reader);
            reader.Close();
            return data;
        }

        private ASyncMailData ReadASyncMailDataFields(IDataReader reader) {
            ASyncMailData data = new ASyncMailData();
            data.Object = Convert.ToString(reader["Object"]);
            data.ReceiverMail = Convert.ToString(reader["ReceiverMail"]);
            data.ReceiverDisplayName = Convert.ToString(reader["ReceiverDisplayName"]);
            data.RequestId = Convert.ToInt32(reader["RequestId"]);
            data.SetTime = Convert.ToDateTime(reader["SetTime"]);
            data.TemplateId = Convert.ToInt32(reader["TemplateId"]);
            data.Type = Convert.ToString(reader["Type"]);
            data.MessageTitle = Convert.ToString(reader["MessageTitle"]);
            return data;
        }

        #endregion

    }
}
